package com.emser.unc.server;

import java.util.ArrayList;
import java.util.List;

import com.emser.unc.client.service.CustomerService;
import com.emser.unc.serializable.CustomerDTO;
import com.emser.unc.serializable.ResultDTO;
import com.emser.unc.shared.exception.NotLoggedInException;
import com.google.gwt.user.server.rpc.RemoteServiceServlet;

public class CustomerServiceImpl extends RemoteServiceServlet implements CustomerService{	
	private static final long serialVersionUID = 1L;
	
		
	@Override
	public boolean addCustomer(String userAName, CustomerDTO customerDTO)
			throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			return AppLibCustomer.addCustomerDataStore(userAName, customerDTO);
		}
	}

	@Override
	public ResultDTO<CustomerDTO> getCustomers(String cursorString, int range,String userAName)
			throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			return AppLibCustomer.getCustomersDataStore(cursorString, range,userAName);
		}
	}

	@Override
	public boolean deleteCustomers(String userAName, ArrayList<Long> customerIds) throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			return AppLibCustomer.deleteCustomersDataStore(userAName, customerIds);
		}
	}

	@Override
	public boolean updateCustomer(String userAName, CustomerDTO customer)	throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			return AppLibCustomer.updateCustomerDataStore(userAName,customer);
		}
	}
	@Override
	public ResultDTO<CustomerDTO> getCustomersSeller(String cursorString, int range,String userAName, String sellerKey) throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			ResultDTO<CustomerDTO> resultData = AppLibCustomer.getCustomersDataStore(cursorString, range,userAName);
			ResultDTO<CustomerDTO> pseller= new ResultDTO<CustomerDTO>();
			pseller.setList(resultData.getList());
			pseller.setCursorString(resultData.getCursorString());
			pseller.setSelectedIDs((ArrayList<Long>) AppLibSeller.getSellerCustomersId(sellerKey));
			return pseller;
		}
	}
	@Override
	public void addCustomersSellerList(String userAName,List<Long> customersIds,
			String sellerKey) throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			AppLibCustomer.addCustomersSeller(customersIds, sellerKey,userAName);
		}
		
	}
	@Override
	public Integer getCustomersCount(String userAName)
			throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			return AppLibCustomer.getCustomersCount(userAName);
		}
		
	}

	@Override
	public ResultDTO<CustomerDTO> getSearchCustomers(String cursorString, int range, ArrayList<String> searchParam)
			throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			return AppLibCustomer.getSearchCustomers(cursorString,range,searchParam);
		}
	}

	@Override
	public CustomerDTO getCustomer(Long idCustomer, String userAName)
			throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{			
			return AppLibCustomer.getCustomer(idCustomer);
		}
	}

	@Override
	public Integer getSearchedCustomersCount(ArrayList<String> searchParam)
			throws NotLoggedInException {
		if(!LoginHelper.isLoggedIn(getThreadLocalRequest())){
			throw new NotLoggedInException("Not logged in.");
		}else{
			return AppLibCustomer.getSearchedCustomersCount(searchParam);
		}
	}

}
